package com.chifaner.tfs.token.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.chifaner.tfs.token.entry.AppInfo;
import com.chifaner.tfs.token.service.AppInfoService;

import comchifaner.util.JsonResult;

@Controller
@RequestMapping(value = "/app/")
public class AppInfoController {

	@Autowired
	private AppInfoService appInfoService;
	
	@RequestMapping(value = "/addApp", method = { RequestMethod.GET, RequestMethod.POST })
	public String addInfo(@RequestParam(value = "name") String name,@RequestParam(value = "type")Integer type,
			String description){
		AppInfo info = new AppInfo();
		info.setName(name);
		info.setType(type);
		info.setDescription(description);
		info.setCreateTime(new Date());
		appInfoService.addInfo(info);
		return "SUCCESS";
	}
	
	
	@RequestMapping(value = "/info", method = { RequestMethod.GET, RequestMethod.POST })
	public String getInfo(HttpServletRequest req,HttpServletResponse res,Integer oid){
		AppInfo appInfo=appInfoService.findById(oid);
		req.setAttribute("data", appInfo);
		try {
			res.setCharacterEncoding("UTF-8"); 
			res.getWriter().write(JsonResult.successWithData(appInfo));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value = "/delete", method = { RequestMethod.GET, RequestMethod.POST })
	public String delete(HttpServletRequest req,HttpServletResponse res,Integer oid){
		appInfoService.delete(oid);
		req.setAttribute("state","success");
		return null;
	}
	
	@RequestMapping(value = "/List", method = { RequestMethod.GET, RequestMethod.POST })
	public String getList(HttpServletRequest req,HttpServletResponse res){
		List<AppInfo> appList=appInfoService.getList(null);
		
		req.setAttribute("dataList", appList);
		return "appManager";
	}
}
